﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AresCustomYacht
{
    public partial class P_WorkType : System.Web.UI.Page
    {
        readonly BL _bl = new BL();
        private int _mode = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            var locationId = int.Parse(Utility.GetQueryStringItem(this, "ParentId"));
            Utility.DisplayPageMessage(this, lblMessage);

            _mode = Utility.GetPageMode(this);
            if (_mode == 1) //Data Mode
            {
                btnNext.Visible = false;

            }
            else
            {
                btnNext.Visible = true;
            }


            if (!Page.IsPostBack)
            {
                var tasks = _bl.GetLocationTasks(locationId);
                tasks.ForEach(p=> p.Name = p.Code + "  " + p.Name);
                lstTask.DataSource = tasks;
                lstTask.DataTextField = "Name";
                lstTask.DataValueField = "Id";
                lstTask.DataBind();
                lstTask.SelectedIndex = 0;
            }
            

        }

        protected void lstType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnNext_Click(object sender, EventArgs e)
        {

        }

        protected void btnAddBudgetMaterial_Click(object sender, EventArgs e)
        {
            GotoAddCost("P-BudgetMaterial.aspx");
        }

        private void GotoAddCost(string landingPage)
        {
            var breadCrump = Utility.GetQueryStringItem(this, "Message");
            var boatId = int.Parse(Utility.GetQueryStringItem(this, "BoatId"));
            var linkAddress =
                "Mode=0&BoatId=" + boatId + "&ParentId=" + lstTask.SelectedValue + "&Message=" + breadCrump + ">" +
                lstTask.SelectedItem.Text;
            if (dffBookmark.Checked)
            {
                linkAddress += "&Bookmark=P-Task.aspx&BookmarkId=" +
                               int.Parse(Utility.GetQueryStringItem(this, "ParentId"));
            }
            else if (!string.IsNullOrEmpty(Utility.GetQueryStringItem(this, "Bookmark")))
            {
                linkAddress += "&Bookmark=" + Utility.GetQueryStringItem(this, "Bookmark") + "&BookmarkId=" +
                               Utility.GetQueryStringItem(this, "BookmarkId");
            }
            Response.Redirect(landingPage + "?k=" + Utility.Encrypt(linkAddress));
        }

        protected void btnAddBudgetLabor_Click(object sender, EventArgs e)
        {
            GotoAddCost("P-BudgetLabor.aspx");
        }

        protected void btnAddActualMaterial_Click(object sender, EventArgs e)
        {
            GotoAddCost("P-ActualMaterial.aspx");
        }

        protected void btnAddActualLabor_Click(object sender, EventArgs e)
        {
            GotoAddCost("P-ActualLabor.aspx");
        }

    }
}